import {getCfgList, getOpenapiLicenseDisable, getOpenapiLicenseEnable, getOpenapiLicenseRuleList} from "@/api/api";
import {toTree} from "@/sever/config";
export const openApi = {
    data() {
        return {
            openLicense:[],//基本信息列表
            openLicenseList:[],//缓存基本信息列表
            allRuleApiList:[],//所有授权api
            allRuleApiTree:[],//所有授权api
            defaultProps:[],//默认选中
            checkedList:[],//应用授权api
        }
    },
    methods: {
        //添加基本信息
        setOpenLicense(){
            let obj = {
                appKey : '',
                appSecret : '',
                remark : '',
            }
            this.openLicense = [...this.openLicense,obj];
            this.openLicenseList = [...this.openLicense,obj];
        },
        //删除基本信息
        delOpenLicense(index,list){
            list.splice(index,1);
            this.openLicense = list;
            this.openLicenseList = list;
        },
        // 开关切换
        switchChange(key,flag){
            if(flag){
                getOpenapiLicenseEnable(key)
                    .then(res => {
                        if (res.code == 200) {
                            this.$Message.success("操作成功");
                            this.setLocalStorage();
                        } else {
                            this.$Message.warning(res.msg);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    });
            }else{
                getOpenapiLicenseDisable(key)
                    .then(res => {
                        if (res.code == 200) {
                            this.$Message.success("操作成功");
                            this.setLocalStorage();
                        } else {
                            this.$Message.warning(res.msg);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    });
            }
        },
        // 获取项目应用授权(所有)
        getRuleAllList(){
            this.allRuleApiTree = [];
            getOpenapiLicenseRuleList().then((res) => {
                if(res.code == 200){
                    let list = res.data.list;
                    let arr = [];
                    list.forEach(v => {
                        let obj = {
                            id: v.ruleId,
                            pid:v.pid,
                            ruleId:v.ruleId,
                            label: v.ruleName,
                        }
                        arr.push(obj);
                    });
                    this.allRuleApiList = arr;
                    this.allRuleApiTree = toTree(arr);
                }else{
                    this.$Message.warning(res.msg)
                }
            }).catch((error) => {
                console.log(error)
            });
        },
        // 查询项目应用授权(应用)
        getOpenLicenseRule(){
            this.checkedList = [];
            for (let i = 0; i < this.allRuleApiTree.length; i++) {
                const e = this.allRuleApiTree[i];
                this.$refs.tree.setChecked(e.id, false, true)
            }
            let params = {
                deptId:this.deptId,
                module:'open_license_rule',
            }
            getCfgList(params).then((res) => {
                if(res.code == 200){
                    let list = res.data.list;
                    list.forEach(v => {
                        this.checkedList.push(v.ruleId)
                        this.$refs.tree.setChecked(v.ruleId, true, false)
                    })
                    localStorage.setItem('checkedList',JSON.stringify(this.checkedList));
                }else{
                    this.$Message.warning(res.msg)
                }
            }).catch((error) => {
                console.log(error)
            });
        },
        // 设置项目应用授权(应用)
        setOpenLicenseRule(){
            let ruleIdList = this.$refs.tree.getCheckedKeys() || [];
            let params = {
                deptId:this.deptId,
                module:'open_license_rule',
                data:ruleIdList
            }
            this.setCfgConfigureFn(params);
        },
    },
    mounted() {
        this.getRuleAllList();
    }
}
